/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThreadScreen.java
 *
 * Created on 23/04/2011, 18:36:03
 */
package implementation.gui.screens;

import java.util.Collection;
import java.util.Iterator;

import javax.swing.JOptionPane;


import implementation.client.EUserParam;
import implementation.client.IUserInput.EUserAction;

import implementation.client.IUserInput;
import implementation.shared.stubs.IPostStub;

/**
 *
 * @author Maxim
 */
public class ThreadScreen extends javax.swing.JFrame {

	
	private  IUserInput _userOutputInput;
	private Object _monitor;
	
	
	private int _selectedTopicId;

	private String[] _postsTopics;
	private int[] _postsIDs;
   
	
    /** Creates new form ThreadScreen */
    public ThreadScreen(IUserInput userOutputInput, Object monitor, Collection<IPostStub> posts) {
    	
    	this._selectedTopicId = -1;
    	this._userOutputInput = userOutputInput;
    	this._monitor = monitor;  
    	
    	int tLoopCounter = 0;
    	
    	this._postsIDs = new int[posts.size()];
    	this._postsTopics = new String[posts.size()];
    	
    	
        for(Iterator<IPostStub> iter = posts.iterator(); iter.hasNext();){
        	IPostStub tPostStub = iter.next();
        	
        	this._postsTopics[tLoopCounter] = tPostStub.getPostTopic();
        	this._postsIDs[tLoopCounter] = tPostStub.getPostId();
        	tLoopCounter++;
        }
    	
    	
    	
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        posts = new javax.swing.JList(this._postsTopics);
        jLabel1 = new javax.swing.JLabel();
        showPost = new javax.swing.JButton();
        profile = new javax.swing.JButton();
        logout = new javax.swing.JButton();
        addPost = new javax.swing.JButton();
        removeThread = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        back = new javax.swing.JButton();
        exit = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jPanel1.setPreferredSize(new java.awt.Dimension(450, 350));


        posts.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                postsValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(posts);

        jLabel1.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        jLabel1.setText("Posts Topics:");

        showPost.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        showPost.setText("Show Post");
        showPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showPostActionPerformed(evt);
            }
        });

        profile.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        profile.setText("Show profile");
        profile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                profileActionPerformed(evt);
            }
        });

        logout.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        logout.setText("Logout");
        logout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutActionPerformed(evt);
            }
        });

        addPost.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        addPost.setText("Add Post");
        addPost.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPostActionPerformed(evt);
            }
        });

        removeThread.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11)); // NOI18N
        removeThread.setText("RemoveThread");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(addPost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(showPost, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(removeThread, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(profile, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(logout, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addComponent(jLabel1))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(showPost)
                        .addGap(18, 18, 18)
                        .addComponent(addPost)
                        .addGap(20, 20, 20)
                        .addComponent(removeThread)
                        .addGap(23, 23, 23)
                        .addComponent(profile)
                        .addGap(18, 18, 18)
                        .addComponent(logout))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        jLabel2.setFont(new java.awt.Font("Tempus Sans ITC", 0, 14));
        jLabel2.setText("Malda Community:");

        back.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11));
        back.setText("Back");
        back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backActionPerformed(evt);
            }
        });

        exit.setFont(new java.awt.Font("Tempus Sans ITC", 0, 11));
        exit.setText("Exit");
        exit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(307, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 412, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(back)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 306, Short.MAX_VALUE)
                        .addComponent(exit)
                        .addGap(38, 38, 38))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(back)
                    .addComponent(exit))
                .addGap(24, 24, 24))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void postsValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_postsValueChanged
        
        this._selectedTopicId = this._postsIDs[this.posts.getSelectedIndex()];
    }//GEN-LAST:event_postsValueChanged

    private void showPostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showPostActionPerformed
        
        if(this._selectedTopicId == -1){
            JOptionPane.showMessageDialog(null, "No topic selected! ", null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        this._userOutputInput.setSelectedAction(EUserAction.EGetPost);
        this._userOutputInput.setParam(EUserParam.EID, this._selectedTopicId);
        
        
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
}//GEN-LAST:event_showPostActionPerformed

    private void profileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_profileActionPerformed
        
        
        this._userOutputInput.setSelectedAction(EUserAction.EProfile);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
}//GEN-LAST:event_profileActionPerformed

    private void logoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutActionPerformed
        
        
        this._userOutputInput.setSelectedAction(EUserAction.ELogout);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
    }//GEN-LAST:event_logoutActionPerformed

    private void addPostActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addPostActionPerformed
        
        this._userOutputInput.setSelectedAction(EUserAction.EAddPost);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();	
    	
    	
    	
}//GEN-LAST:event_addPostActionPerformed

    private void backActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backActionPerformed
        
        this._userOutputInput.setSelectedAction(EUserAction.EBack);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
}//GEN-LAST:event_backActionPerformed

    private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
        
        
        this._userOutputInput.setSelectedAction(EUserAction.EExit);
        
        synchronized(this._monitor){
            this._monitor.notify();
        }
        this.dispose();
    }//GEN-LAST:event_exitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addPost;
    private javax.swing.JButton back;
    private javax.swing.JButton exit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton logout;
    private javax.swing.JList posts;
    private javax.swing.JButton profile;
    private javax.swing.JButton removeThread;
    private javax.swing.JButton showPost;
    // End of variables declaration//GEN-END:variables
}
